<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\Admin\Models\UcenterMemberModel;
use App\FreeBird\Contracts\VipShopInterface;
use App\FreeBird\Exceptions\FreeBirdException;
use App\FreeBird\Models\UserVipPayModel;
use App\FreeBird\Models\VipShopModel;
use App\Pay\Repositories\DbWeixinPayRepositories;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;

class  DbVipshopRepositories implements VipShopInterface
{
    /**
     * 模型
     * @return VipShopModel
     */
    public function getVipShopModel(){
        return app()->make(VipShopModel::class);
    }


    /**
     * vip支付表
     * @return UserVipPayModel
     */
    public function getUserVipPayModel(){
        return app()->make(UserVipPayModel::class);
    }

    /**
     * 微信操作类
     * @return DbWeixinPayRepositories
     */
    public function getDbWeixinPayRepositories(){
        return app()->make(DbWeixinPayRepositories::class);
    }

    /**
     * 用户中心
     * @return UcenterMemberModel
     */
    public function getUcenterMemberModel(){
        return app()->make(UcenterMemberModel::class);
    }

    /**
     * 用户表
     * @return WeixinUserModel
     */
    public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
    }


    /**
     * 列表 分页
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        return $this->getVipShopModel()->newQuery()->select(["id","title","amount","month","list_order","create_time","status"])->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * 列表--不分页
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getListAll($where){

        $request=Request::instance();
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        return $this->getVipShopModel()->newQuery()->select(["id","title","amount",'lid',"month","list_order","create_time","status"])->where($where)->orderBy($sort,$order)->get();
    }

    /**
     * 保存数据
     * @param $data
     * @return mixed|void
     */
    public function saveData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        $data["uid"]=UID;
        return $this->getVipShopModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed|void
     */
    public function getInfo($id){
        return $this->getVipShopModel()->newQuery()->where("id",$id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where){
        return $this->getVipShopModel()->newQuery()->where($where)->delete();
    }

    /**
     * vip购买支付
     * @param $data
     * @return mixed|void
     */
    public function setUserBuyVip($data){
        $data["pay_sn"]=$this->getUserVipPayModel()->makePaySn(AUID);
        $data["uid"]=AUID;
        $info=$this->getInfo($data['pay_id']);
        $data["pay_amount"]=$info['amount'];
        $openId=$this->getUcenterMemberModel()->newQuery()->where("id",AUID)->value("username");
        $data['lid']=$this->getWeixinUserModel()->newQuery()->where("uid",AUID)->value("lid");
        if($this->getUserVipPayModel()->newQuery()->create($data)){
            /**
             * 调起支付
             */
            $notify_url=get_config_param_value('XIAO_WXPAY_MONTH_NOTIFY');//config("lzy.wxpay_notify");  //支付成功后通知地址
            return $this->getDbWeixinPayRepositories()->getWeixinJsPay($data["pay_sn"],$data['pay_amount'],$openId,$notify_url,$info['title']);
        }
    }

    /**
     * 支付返回通知
     * @param $xml
     * @return mixed|void
     */
    public function setVipBuyNotify($xml){
       //支付成功
       $returnData=$this->getDbWeixinPayRepositories()->checkNotity($xml);
       $info=$this->getUserVipPayModel()->newQuery()->where("pay_sn",$returnData["out_trade_no"])->where("order_status","10")->first();
       if(empty($info)){
           //无此订单不用在响应
           throw  new FreeBirdException(FreeBirdException::EROOR_INFO_NOT_FIND);
       }
       if($this->getUserVipPayModel()->buyVipUserData($info)){
           $arr=array();
           $arr["return_code"]="SUCCESS";
           $arr["return_msg"]="OK";
           echo  $this->arrayToXml($arr);  //支付成功返回
           exit;
       }else{
           $arr=array();
           $arr["return_code"]="Fail";
           $arr["return_msg"]="购买vip错误-->支付号".$returnData["out_trade_no"];
           echo  $this->arrayToXml($arr);  //支付成功返回
           exit;
       }
       Log::error("Pay......setVipBuyNotify...".$xml);  //记录错误信息
    }
    //数组转xml
    public function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }

    /**
     * 用户金币完成
     * @param $data
     * @return mixed
     * @throws FreeBirdException
     */
    public function setUserScoreBuyVip($data){
        $data["uid"]=AUID;
        $data['lid']=$this->getWeixinUserModel()->newQuery()->where("uid",AUID)->value("lid");
        return $this->getUserVipPayModel()->buyVipUserScoreData($data);
    }



}